layui.use(['jquery','layer','table','form'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var btn = 0;
    getTable('','','','');
    countNum();
    //按钮样式
    $('.btn_style').on('click',function () {
        $(this).addClass('btn_onclick').siblings().removeClass('btn_onclick');
        $(this).removeClass('btn_remove').siblings().addClass('btn_remove');
    })
    //课程分类赋值
    $.ajax({
        url: '/examine/getCourseClass',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            var courseClass = data.data;
            for (var i = 0; i < courseClass.length; i++) {
                $('#courseClass').append('<option value="' + courseClass[i].name + '" >' + courseClass[i].name + '</option>')
            }
            form.render('select');
        }
    });
    //高级检索
    $('#advancedSearching').on('click',function () {
        layer.open({
            type: 2,
            title: '高级检索',
            content: ['advanced-search.html','no'],
            area: [ '690px', '460px'],
            offset: 'auto',
            shade: 0.4 ,
            resize: false
        })
    });
    //筛选搜索
    $('#search').on('click',function () {
        var courseName = $("input[name=courseName]").val();
        var courseClass = $("#courseClass option:selected").text();
        var createName = $("input[name=createName]").val();
        getTable(courseName,courseClass,createName,'');
        $('#examine').addClass('btn_onclick').siblings().removeClass('btn_onclick');
        $('#examine').removeClass('btn_remove').siblings().addClass('btn_remove');
    })
    //点击按钮
    $('#examine').on('click',function () {
        btn = 0;
        getTable('','','','','')
    })
    $('#approved').on('click',function () {
        btn = 1;
        getTable('','','',1,1)
    })
    $('#audit').on('click',function () {
        btn = 2;
        getTable('','','',2,2)
    })
    $('#auditFailed').on('click',function () {
        btn = 3;
        getTable('','','',3,3)
    })
    //获取审核次数
    function countNum() {
        $.ajax({
            url:'/examine/getExamineNum',
            type:'post',
            dataType:'json',
            data:{'type':2},
            success:function (data) {
                $('#allNum').text(data.data[0].allNum);
                $('#approvedNum').text(data.data[0].approvedNum);
                $('#auditNum').text(data.data[0].auditNum);
                $('#auditFailedNum').text(data.data[0].auditFailedNum);
            }
        })
    }
    //高级检索获取数据
    advancedSearch = function advancedSearch(data){
        dataTable(data);
    }
    //获取表格数据
    function getTable(courseName,courseClass,createName,courseCstatus,result) {
        $.ajax({
            url:'/examine/getCourses',
            type:'post',
            dataType:'json',
            data:{'courseName':courseName,'courseClass':courseClass,'createName':createName,'courseCstatus':courseCstatus,'result':result,'type':2},
            success:function (data) {
                dataTable(data.data);
            }
        })
    }
    //表格渲染
    function dataTable(data) {
        table.render({
            elem:'#demoTable',
            title:"课程审核列表",
            data: data,
            toolbar:'#tableHeader',
            limit:10,
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                limits:[10,20,30],
                first:'首页',
                last:'尾页',
            },
            cols:[[
                {title:'序号',align:'center',templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:'courseId',title:'课程编号',align:'center'},
                {field:'courseName',title:'课程名称',align:'center'},
                {field:'',title:'课程图片',align:'center',templet:function(data){
                        return '<img src="'+ data.url +'">';
                    }},
                {field:'coursePrice',title:'原价',align:'center',templet:function(data){
                        return '原价：' + data.coursePrice;
                    }},
                {field:'courseNprice',title:'现价',align:'center',templet:function(data){
                        return '现价：' + data.courseNprice;;
                    }},
                {field:'courseClass',title:'分类',align:'center'},
                {field:'createName',title:'上传人员',align:'center'},
                {field:'result',title:'审核状态',align:'center',templet:function(data){
                        if(data.result == 1){
                            return '<p>审核通过</p>';
                        }else if(data.result == 2){
                            return '<p>待审核</p>';
                        }else if(data.result == 3){
                            return '<p>未通过</p>';
                        }
                    }},
                {title:'操作',align:'center',templet:function(data){
                        if(data.result == 1 || data.result == 3){
                            return '<a onclick="javascript:checkTable('+ data.courseId +');">查看</a>';
                        }else if(data.result == 2){
                            return '<a onclick="javascript:checkTable('+ data.courseId +');">查看</a>&nbsp;&nbsp;<a onclick="javascript:audit('+ data.courseId + "," + data.recordId +');">审核</a>';
                        }
                    }}
            ]]
        })
    }
    //查看课程详情
    checkTable = function checkTable(id) {
        $.ajax({
            url: '/examine/getCourseInformation',
            type: 'post',
            dataType: 'json',
            data: {'type':2,'id':id},
            success: function (data) {
                localStorage.setItem('course',JSON.stringify(data.data));
                layer.open({
                    type:2,
                    title:"课程详情",
                    area:['95%','100%'],
                    Boolean:true,
                    maxmin:true,
                    closeBtn:1,
                    btn:["确定","取消"],
                    offset:['0px','35px'],
                    content:"/examine/course/course-details.html",
                    success: function (layero,index) {
                        layero.find('.layui-layer-max').remove();
                        layero.find('.layui-layer-min').remove();
                        var child = layero.find('iframe').contents();
                    }
                })
            }
        })
    }
    //审核
    audit = function audit(courseId,recordId) {
        console.log(courseId,recordId);
        localStorage.setItem('courseId',JSON.stringify(courseId));
        localStorage.setItem('recordId',JSON.stringify(recordId));
        layer.open({
            type:2,
            title:'审核课程',
            content:'/examine/course/course-audit.html',
            area:['752px','400px'],
            offset:'auto',
            shade:0.4,
            success: function(layero,index){
            },
            end: function () {
                getTable('','','','');
                countNum();
            }
        });
    }
    //刷新
    $('#refresh').click(function () {
        location.reload();
    })
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });

})